package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Point;
import android.media.RemoteControlClient;
import android.os.Bundle;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

import overlayutil.PoiOverlay;

public class LBS extends AppCompatActivity {

    private MapView mMapView = null;
    private BaiduMap mBaiduMap=null;
    private LocationClient mLocationClient=null;
    private PoiSearch mPoiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_l_b_s);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);

        mBaiduMap=mMapView.getMap();

        /*
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.zoom(18.0f);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));*/

        mBaiduMap.setMyLocationEnabled(true);
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

/*
        //定位初始化
        mLocationClient = new LocationClient(this);

        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);

        //设置locationClientOption
        mLocationClient.setLocOption(option);
        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
        //开启地图定位图层
        mLocationClient.start();



        //定位模式 地图SDK支持三种定位模式：NORMAL（普通态）, FOLLOWING（跟随态）, COMPASS（罗盘态）

        MyLocationConfiguration.LocationMode mCurrentMode;
        mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;//定位跟随态
        //mCurrentMode = LocationMode.NORMAL;   //默认为 LocationMode.NORMAL 普通态
        //mCurrentMode = LocationMode.COMPASS;  //定位罗盘态

        //自定义定位图标 支持自定义定位图标样式，替换定位icon
        BitmapDescriptor mCurrentMarker;
        mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.geo_icon);

        //自定义精度圈填充颜色
        int accuracyCircleFillColor = 0xAAFFFF88;

        //自定义精度圈边框颜色
        int accuracyCircleStrokeColor = 0xAA00FF00;

        MyLocationConfiguration mLocationConfiguration=new MyLocationConfiguration(mCurrentMode,true,mCurrentMarker,accuracyCircleFillColor,accuracyCircleStrokeColor);
        mBaiduMap.setMyLocationConfiguration(mLocationConfiguration);
        /*
        mBaiduMap.setTrafficEnabled(true);
        mBaiduMap.setCustomTrafficColor("#ffba0101", "#fff33131", "#ffff9e19", "#00000000");
        //  对地图状态做更新，否则可能不会触发渲染，造成样式定义无法立即生效。
        MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(13);
        mBaiduMap.animateMapStatus(u);*/

        //开启热力图
        //mBaiduMap.setBaiduHeatMapEnabled(true);

        mMapView.setLogoPosition(LogoPosition.logoPostionRightTop);


        UiSettings mUiSettings=mBaiduMap.getUiSettings();
        mUiSettings.setCompassEnabled(true);


        //地图Logo不允许遮挡，可通过以下方法可以设置地图边界区域，来避免UI遮挡。

        //mBaiduMap.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);

        //其中参数paddingLeft、paddingTop、paddingRight、paddingBottom参数表示距离屏幕边框的左、上、右、下边距的距离，单位为屏幕坐标的像素密度。

         //mMapView=new MapView(this);
         //setContentView(mMapView);

        /*
       // 定义BaiduMapOptions对象
        BaiduMapOptions options = new BaiduMapOptions();
       // 设置需要的状态

        //设置地图模式为卫星地图
        options.mapType(BaiduMap.MAP_TYPE_SATELLITE);


       // 创建MapView对象
        mMapView = new MapView(this, options);
       // 添加MapView对象
        setContentView(mMapView);
        */

        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
        mPoiSearch.searchInCity((new PoiCitySearchOption())
                .city("北京")
                .keyword("沙县小吃")
                .pageNum(10));



    }



    private class MyPoiOverlay extends PoiOverlay {

        public MyPoiOverlay(BaiduMap baiduMap) {
            super(mBaiduMap);
        }

        @Override

        public boolean onPoiClick(int index) {
            super.onPoiClick(index);
            return true;
        }
    }


    OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
        @Override
        public void onGetPoiResult(PoiResult result) {
            //获取POI检索结果
            if (result == null
                    || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果

                return;
            } else if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                if (result != null) {

                    mBaiduMap.clear();

                    //创建PoiOverlay

                    PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);

                    //设置overlay可以处理标注点击事件

                    mBaiduMap.setOnMarkerClickListener(overlay);

                    //设置PoiOverlay数据

                    overlay.setData(result);

                    //添加PoiOverlay到地图中

                    overlay.addToMap();
                    overlay.zoomToSpan();
                    return;
                }

                mPoiSearch.destroy();
        }
        }

        @Override
        public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

        }

        @Override
        public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

        }

        @Override
        public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

        }
    };


    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
        }
    }

    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        //mLocationClient.stop();
        //mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }
}